//
//  ScenicDatabaseManager.m
//  TravelToHelp
//
//  Created by lanou3g on 15/11/7.
//  Copyright © 2015年 冯贵宾. All rights reserved.
//

#import "ScenicDatabaseManager.h"
#import <sqlite3.h>
#import "FMDB.h"

@interface ScenicDatabaseManager ()

@property (nonatomic, strong) FMDatabase * database;

@end

@implementation ScenicDatabaseManager

+ (instancetype)shareManager
{
    static ScenicDatabaseManager *manager;
    static dispatch_once_t oneToken;
    dispatch_once(&oneToken, ^{
        manager = [[ScenicDatabaseManager alloc] init];
        // 获取路径
        NSString * documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
        NSString *dbPath = [documentsPath stringByAppendingPathComponent:@"ScenicDetail.sqlite"];
     
        // 初始化FMDB
         manager.database = [FMDatabase databaseWithPath:dbPath];
        [manager createTable];
    });
    return manager;
}

- (void)createTable
{
    [self.database open];
    [self.database executeUpdate:@"create table user (Address text, Introduction text, Name text, OpenTime text, TicketsPrice text, Foreword text, SharePic text)"];
   
    [self.database close];
}

- (void)insertDatabaseWithModel:(ScenicDetailModel *)model
{
    [self.database open];
    
    FMResultSet *rs =  [self.database executeQuery:@"insert into user(Address,Introduction,Name,OpenTime,TicketsPrice,Foreword,SharePic) values(?,?,?,?,?,?,?)" withArgumentsInArray:@[model.Address,model.Introduction,model.Name,model.OpenTime,model.TicketsPrice,model.Foreword,model.SharePic]];
    [rs next];

    [self.database close];

}

- (void)deleteByName:(NSString *)name
{
    [self.database open];
    [self.database executeUpdateWithFormat:@"delete from user where Name = %@",name];
   
    [self.database close];
}

- (ScenicDetailModel *)selectModelByName:(NSString *)name
{
    [self.database open];
    
    NSString *Name = [self.database stringForQuery:@"select Name from user where Name = ?",name];
     NSString *Address = [self.database stringForQuery:@"select Address from user where Name = ?",name];
     NSString *Introduction = [self.database stringForQuery:@"select Introduction from user where Name = ?",name];
     NSString *OpenTime = [self.database stringForQuery:@"select OpenTime from user where Name = ?",name];
     NSString *TicketsPrice = [self.database stringForQuery:@"select TicketsPrice from user where Name = ?",name];
     NSString *Foreword = [self.database stringForQuery:@"select Foreword from user where Name = ?",name];
     NSString *SharePic = [self.database stringForQuery:@"select SharePic from user where Name = ?",name];
    ScenicDetailModel *model = [[ScenicDetailModel alloc] init];
    model.Name = Name;
    model.Introduction = Introduction;
    model.OpenTime = OpenTime;
    model.Address = Address;
    model.Foreword = Foreword;
    model.TicketsPrice = TicketsPrice;
    model.SharePic = SharePic;
     [self.database close];
    return model;
}

- (NSMutableArray *)selectAllModels
{
    [self.database open];
    NSMutableArray *modelArray = [NSMutableArray array];
    //查询整个表
    FMResultSet * resultSet = [self.database executeQuery:@"select * from user"];
    
    //[resultSet next]遍历条件 遍历前为YES 遍历结束为NO
    while ([resultSet next] == YES)
    {
        //         用[resultSet stringForColumn:]或[resultSet intForColumn:]等方法把值转成Object-C的型态
    NSString * Name = [resultSet stringForColumn:@"Name"];
    NSString * Address = [resultSet stringForColumn:@"Address"];
 NSString * Introduction = [resultSet stringForColumn:@"Introduction"];
    NSString * OpenTime = [resultSet stringForColumn:@"OpenTime"];
    NSString * Foreword = [resultSet stringForColumn:@"Foreword"];
 NSString * TicketsPrice = [resultSet stringForColumn:@"TicketsPrice"];
    NSString * SharePic = [resultSet stringForColumn:@"SharePic"];
    ScenicDetailModel *model = [[ScenicDetailModel alloc] init];
        model.Name = Name;
        model.Introduction = Introduction;
        model.OpenTime = OpenTime;
        model.Address = Address;
        model.Foreword = Foreword;
        model.TicketsPrice = TicketsPrice;
        model.SharePic = SharePic;
        [modelArray addObject:model];
    }
    [self.database close];
    return modelArray;
}




@end






